package domain.entities;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Guillermo
 * Date: 12/09/12
 * Time: 11:47
 * To change this template use File | Settings | File Templates.
 */
public class ProtocolLevelDocumentationDAO extends HibernateDaoSupport {

    private static final Logger log = LoggerFactory
            .getLogger(ProtocolLevelDocumentationCategoryDAO.class);

    protected void initDao() {
        // do nothing
    }

    public List findAll() {
        log.debug("finding all ProtocolLevelDocumentation instances");
        try {
            String queryString = "from ProtocolLevelDocumentation";
            return getHibernateTemplate().find(queryString);
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public void save(ProtocolLevelDocumentation transientInstance) {
        log.debug("saving ProtocolLevelDocumentation instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void delete(ProtocolLevelDocumentation persistentInstance) {
        log.debug("deleting ProtocolLevelDocumentation instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public ProtocolLevelDocumentation findById(Integer id) {
        log.debug("getting ProtocoloCentro instance with id: " + id);
        try {
            ProtocolLevelDocumentation instance = (ProtocolLevelDocumentation) getHibernateTemplate()
                    .get("domain.entities.ProtocolLevelDocumentation", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public void attachDirty(ProtocolLevelDocumentation instance) {
        log.debug("attaching dirty ProtocolLevelDocumentation instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(ProtocolLevelDocumentation instance) {
        log.debug("attaching clean ProtocolLevelDocumentation instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }



    public List findByProtocoloId(String protocoloId, Integer compromisopaisId) {
        try {
            String queryString = "select mp from ProtocolLevelDocumentation mp inner join mp.protocoloCompromisopaises pcp inner join pcp.protocolo p where p.id = '" + protocoloId +"' and pcp.compromisopais.id = " + compromisopaisId +" group by mp.id";
            return getHibernateTemplate().find(queryString);
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public static ProtocolLevelDocumentationDAO getFromApplicationContext(
            ApplicationContext ctx) {
        return (ProtocolLevelDocumentationDAO) ctx
                .getBean("ProtocolLevelDocumentationDAO");
    }

}
